# -*- coding: utf-8 -*-

# Scrapy settings for ShopifyMonitor project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     https://docs.scrapy.org/en/latest/topics/settings.html
#     https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#     https://docs.scrapy.org/en/latest/topics/spider-middleware.html
import os
from datetime import datetime

BOT_NAME = 'ShopifyMonitor'

SPIDER_MODULES = ['ShopifyMonitor.spiders']
NEWSPIDER_MODULE = 'ShopifyMonitor.spiders'


# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'ShopifyMonitor (+http://www.yourdomain.com)'

# Obey robots.txt rules
# ROBOTSTXT_OBEY = True

# Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32

# Configure a delay for requests for the same website (default: 0)
# See https://docs.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
#DOWNLOAD_DELAY = 3
# The download delay setting will honor only one of:
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
#CONCURRENT_REQUESTS_PER_IP = 16

# Disable cookies (enabled by default)
#COOKIES_ENABLED = False

# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False

# Override the default request headers:
#DEFAULT_REQUEST_HEADERS = {
#   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#   'Accept-Language': 'en',
#}

# Enable or disable spider middlewares
# See https://docs.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
#    'ShopifyMonitor.middlewares.ShopifymonitorSpiderMiddleware': 543,
#}

# Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
#    'ShopifyMonitor.middlewares.ShopifymonitorDownloaderMiddleware': 543,
#}

# Enable or disable extensions
# See https://docs.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
#    'scrapy.extensions.telnet.TelnetConsole': None,
#}

# Configure item pipelines
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
    # 'ShopifyMonitor.pipelines.ShopifymonitorPipeline': 300,
    #  300指的是pipeline组件的优先级， 数字越小优先级越大。
    'ShopifyMonitor.pipelines.SqlitePipeline': 300,
    'ShopifyMonitor.pipelines.MyImagesPipeline': 1,
    # 使用scrapy自带的pipeline存储图片
    # 'scrapy.pipelines.images.ImagesPipeline': 1
}

# Enable and configure the AutoThrottle extension (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'


# 1. 日志信息的配置
now = datetime.now().strftime('_%Y_%m_%d_%H_%m')
LOG_FILE = os.path.join('logs', 'shopify' + now + '.log')      # 日志存储路径
LOG_LEVEL = "DEBUG"
# LOG_ENABLED = True    # 默认日志是开启的，可以省略
# LOG_FORMAT            # 默认的日志格式'%(asctime)s [%(name)s] %(levelname)s: %(message)s'
# LOG_DATEFORMAT        # 默认的时间格式%Y-%m-%d %H:%M:%S'



# 1. 日志信息的配置
now = datetime.now().strftime('_%Y_%m_%d_%H_%m')
LOG_FILE = os.path.join('logs', 'shopify' + now + '.log')      # 日志存储路径
LOG_LEVEL = "DEBUG"
# LOG_ENABLED = True    # 默认日志是开启的，可以省略
# LOG_FORMAT            # 默认的日志格式'%(asctime)s [%(name)s] %(levelname)s: %(message)s'
# LOG_DATEFORMAT        # 默认的时间格式%Y-%m-%d %H:%M:%S'


# ***************************提升爬虫速度***********************************
# 1). 降低下载延迟
DOWNLOAD_DELAY = 1
# 2). 多线程爬虫(scrapy框架是基于多线程Twisted, 默认并发线程是32个)
CONCURRENT_REQUESTS = 100
CONCURRENT_REQUESTS_PER_DOMAIN = 100        # www.baidu.com, mail.baidu.com
CONCURRENT_REQUESTS_PER_IP = 100            # 1.1.1.1       2.2.2.2
# 3). 禁止使用Cookies，减少CPU的使用率
COOKIES_ENABLED = False
# 4). 网页爬取失败禁止重试
# RETRY_ENABLED = False
# 5). 减少下载超时
DOWNLOAD_TIMEOUT = 50
# 6). 降低日志记录级别，减少CPU的使用率。  LOG_LEVEL = "WARNING"


# *******************************特殊设置*************************************
# 1). 不遵从机器人协议:
ROBOTSTXT_OBEY = False

# 2). 可以使403状态的响应不被自动忽略，可以自响应中对403 的响应做个性化处理
HTTPERROR_ALLOWED_CODES = [403]

# 3). 超时重试
RETRY_ENABLED = True
RETRY_TIMES = 5

# 4). 设置图片存储位置
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
IMAGES_STORE = os.path.join(BASE_DIR, 'images')
# IMAGES_URLS_FIELD = "image"
# 30 days of delay for images expiration
IMAGES_EXPIRES = 30
IMAGES_THUMBS = {
    'small': (50, 50),
    'big': (270, 270),
}
